package com.chilly.other.jdk8.Interface;

import org.junit.Test;

import java.util.Arrays;

//定义函数式接口
@FunctionalInterface
interface ArrayBuilder {
    int[] builderArray(int length);
}

/**
 * @project: jdk-learnig
 * @description: 数组的构造器引用
 * @author: 黄宇辉
 * @date: 8/18/2019-7:51 PM
 * @version: 1.0
 * @website: https://yubuntu0109.github.io/
 */
public class RefArrayConstructor {

    public static int[] getArray(int length, ArrayBuilder arrayBuilder) {
        return arrayBuilder.builderArray(length);
    }

    //传统写法
    @Test
    public void testgetArray() {
        int[] array = getArray(10, new ArrayBuilder() {
            @Override
            public int[] builderArray(int length) {
                return new int[length];
            }
        });
        System.out.println(Arrays.toString(array));
    }

    //Lambda表达式写法
    @Test
    public void testArrayWithLambda() {
        int[] array = getArray(10, length -> new int[length]);
        System.out.println(Arrays.toString(array));
    }

    //方法引用写法
    @Test
    public void testArrayWithMethodRef() {
        int[] array = getArray(10, int[]::new);
        System.out.println(Arrays.toString(array));
    }
}